<!--
 * @Descripttion: 产品品质表单
 * @LastEditors: xzh
 * @LastEditTime: 2023-10-10 20:46:24
-->
<template>
  <DileDrawer
    v-model="productQualityForm.visible"
    :loading="productQualityForm.loading"
    size="60%"
    @sure="hanldeSure"
  >
    <template #header>
      <span>{{
        $t(`table.btnArr.${!productQualityForm.formData.id ? "add" : "edit"}`)
      }}</span>
    </template>
    <template #default>
      <el-form
        ref="formRef"
        :model="productQualityForm.formData"
        :rules="formRules"
        label-width="7rem"
        size="large"
        status-icon
      >
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.inspector')
            "
            prop="code"
          >
            <!-- 输入框选择对象 -->
            <DileInputSelectItem
              v-model="productQualityForm.formData.inspector"
              :selectType="selectTypeEnum.people"
              :cacheLabel="productQualityForm.formData.extJson.inspectorName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.operateTime')
            "
          >
            <el-date-picker
              v-model="productQualityForm.formData.operateTime"
              type="datetime"
              :placeholder="$t('common.select')"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.classes')
            "
          >
            <el-input
              v-model="productQualityForm.formData.classes"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.abnormalManageArr.productQualityAbnormalArr.code')"
          >
            <el-input
              v-model="productQualityForm.formData.code"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.processCode')
            "
          >
            <el-input
              v-model="productQualityForm.formData.relProcess.code"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.processName')
            "
          >
            <!-- 输入框选择对象 -->
            <DileInputSelectItem
              v-model="productQualityForm.formData.process"
              :selectType="selectTypeEnum.processDefine"
              :cacheLabel="productQualityForm.formData.relProcess.name"
              @itemSelected="handleProcessSelected"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t(
                'page.abnormalManageArr.productQualityAbnormalArr.productYield'
              )
            "
          >
            <el-input
              v-model="productQualityForm.formData.productYield"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('page.systemManageArr.productArr.code')">
            <el-input
              v-model="productQualityForm.formData.relProduct.code"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('page.systemManageArr.productArr.name')">
            <!-- 输入框选择对象 -->
            <DileInputSelectItem
              v-model="productQualityForm.formData.product"
              :selectType="selectTypeEnum.product"
              :cacheLabel="productQualityForm.formData.relProduct.name"
              @itemSelected="handleProductSelected"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipManageArr.equipArchivesArr.code')"
          >
            <el-input
              v-model="productQualityForm.formData.relEquip.code"
              disabled
              :placeholder="$t('common.systemAutoBringOut')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('page.equipManageArr.equipArchivesArr.name')"
          >
            <el-input
              v-model="productQualityForm.formData.relEquip.name"
              readonly
              :placeholder="$t('common.select')"
              @click="() => dileSelectItemTableRef.init()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="
              $t('page.abnormalManageArr.productQualityAbnormalArr.content')
            "
          >
            <el-input
              v-model="productQualityForm.formData.content"
              :placeholder="$t('common.enter')"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="$t('common.remark')">
            <el-input
              v-model="productQualityForm.formData.remark"
              :rows="5"
              :placeholder="$t('common.enter') + $t('common.remark')"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-form>
    </template>
  </DileDrawer>
  <!-- 选择对象列表 -->
  <DileSelectItemTable
    ref="dileSelectItemTableRef"
    :selectTableType="selectTableTypeEnum.equip"
    :multiple="false"
    @sure="handleEquipSelected"
  />
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, computed, nextTick } from "vue";
import type { object_Inf } from "@/utils/typings/index.type";
import { useI18n } from "vue-i18n";
import { ElNotification, dayjs } from "element-plus";
import {
  selectTypeEnum,
  selectTableTypeEnum,
} from "@/components/dile-selectItem/index.type";
import DileInputSelectItem from "@/components/dile-selectItem/selectItem-input/index.vue";
import DileSelectItemTable from "@/components/dile-selectItem/selectItem-table/index.vue";
import DileDrawer from "@/components/dile-drawer/index.vue";
import Dile_APi from "@/api";

const emit = defineEmits(["refresh"]);

let { t } = useI18n();
/**
 * @Author: xzh
 * @Descripttion: 选择对象表格实例
 * @Param:
 */
let dileSelectItemTableRef = ref<any>(null);
/**
 * @Author: xzh
 * @Descripttion: 表单默认数据
 * @Param:
 */
let defaultData = () => {
  return {
    id: 0,
    status: true, //状态
    code: "", //产品品质号
    name: "", //模具名称
    inspector: null, //检验员
    operateTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"), //操作时间
    classes: "", //班次
    productYield: "", //产品良率
    //工序信息
    process: null,
    relProcess: {
      code: "",
      name: "",
    },
    //产品信息
    product: null,
    relProduct: {
      code: "",
      name: "",
    },
    //设备信息
    equipmentId: null,
    relEquip: {
      code: "",
      name: "",
    },
    content: "", //异常内容
    remark: "", //备注
    extJson: {
      inspectorName: "", //检验员名称
    },
  };
};
/**
 * @Author: xzh
 * @Descripttion: 产品品质表单参数
 * @Param:
 */
let productQualityForm = reactive<object_Inf>({
  visible: false,
  loading: false,
  //表单数据
  formData: defaultData(),
});

/**
 * @Author: xzh
 * @Descripttion: 表单实例
 * @Param:
 */
let formRef = ref<any>(null);

/**
 * @Author: xzh
 * @Descripttion:表单验证规则
 * @Param:
 * @param {*} computed
 */
let formRules = computed((): any => {
  return reactive({
    code: [
      {
        required: true,
        message:
          t("common.enter") +
          t("page.productOrderManageArr.productOrderArr.code"),
        trigger: "change",
      },
    ],
  });
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 产品品质主键
 */
let init = async (id: number) => {
  let { visible, loading } = toRefs(productQualityForm);
  visible.value = true;
  await nextTick();
  try {
    loading.value = true;
    //加载表单数据
    await initFormData(id);
  } finally {
    loading.value = false;
  }
};

/**
 * @Author: xzh
 * @Descripttion: 加载表单数据
 * @Param:
 */
let initFormData = async (id: number) => {
  let data: object_Inf = !!id
    ? await Dile_APi.getMoldArchivesInfo(id)
    : defaultData();
  Object.assign(productQualityForm.formData, data);
};

/**
 * @Author: xzh
 * @Descripttion: 工序选择
 * @Param:
 * @param {*} dataArr
 */
let handleProcessSelected = (dataArr: Array<object_Inf>) => {
  let process = dataArr.find(() => true);
  Object.assign(productQualityForm.formData.relProcess, process);
};

/**
 * @Author: xzh
 * @Descripttion: 产品选择
 * @Param:
 * @param {*} dataArr
 */
let handleProductSelected = (dataArr: Array<object_Inf>) => {
  let product = dataArr.find(() => true);
  Object.assign(productQualityForm.formData.relProduct, product);
};

/**
 * @Author: xzh
 * @Descripttion: 设备选中事件
 * @Param:
 * @param {*} equip
 */
let handleEquipSelected = (equipArr: Array<object_Inf>) => {
  let { formData } = toRefs(productQualityForm),
    equip: object_Inf = equipArr.find(() => true) || {};
  Object.assign(formData.value, {
    ...formData.value,
    equipmentId: equip.id,
    relEquip: equip,
  });
};

/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { formData, loading, visible } = toRefs(productQualityForm),
    valid = await formRef.value.validate();
  if (!valid || !!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    await Dile_APi.saveMoldArchives(formData.value);
    //触发刷新回调
    emit("refresh");
    //提示成功
    ElNotification({
      message: t("common.message.OperationSuccess"),
      type: "success",
    });
    //关闭弹框
    visible.value = false;
  } finally {
    loading.value = false;
  }
};

defineExpose({
  init,
});
</script>
<style lang="scss" scoped></style>
